package com.ruoyi.mes.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.MtrInfo;
import com.ruoyi.mes.domain.ProInfo;
import com.ruoyi.mes.mapper.MtrInfoMapper;
import com.ruoyi.mes.mapper.ProInfoMapper;
import com.ruoyi.mes.utils.PageInfoUtils;
import com.ruoyi.mes.utils.RepeatUtils;
import com.ruoyi.mes.vo.list.BomListVo;
import com.ruoyi.mes.vo.product.BomMtrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.ProBomMapper;
import com.ruoyi.mes.domain.ProBom;
import com.ruoyi.mes.service.IProBomService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品BOMService业务层处理
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Service
public class ProBomServiceImpl implements IProBomService {

    @Autowired
    private MtrInfoMapper mtrInfoMapper;

    @Autowired
    private ProBomMapper proBomMapper;

    @Autowired
    private ProInfoMapper proInfoMapper;

    /**
     * 查询产品BOM
     *
     * @param id 产品BOM主键
     * @return 产品BOM
     */
    @Override
    public ProBom selectProBomById(Long id) {
        return proBomMapper.selectProBomById(id);
    }

    /**
     * 查询产品BOM列表
     *
     * @param proBom 产品BOM
     * @return 产品BOM
     */
    @Override
    public List<ProBom> selectProBomList(ProBom proBom) {
        return proBomMapper.selectProBomList(proBom);
    }

    /**
     * 新增产品BOM
     *
     * @param proBom 产品BOM
     * @return 结果
     */
    @Override
    public int insertProBom(ProBom proBom) {
        proBom.setCreateTime(DateUtils.getNowDate());
        return proBomMapper.insertProBom(proBom);
    }

    /**
     * 修改产品BOM
     *
     * @param proBom 产品BOM
     * @return 结果
     */
    @Override
    public int updateProBom(ProBom proBom) {
        proBom.setUpdateTime(DateUtils.getNowDate());
        return proBomMapper.updateProBom(proBom);
    }

    /**
     * 批量删除产品BOM
     *
     * @param ids 需要删除的产品BOM主键
     * @return 结果
     */
    @Override
    public int deleteProBomByIds(Long[] ids) {
        return proBomMapper.deleteProBomByIds(ids);
    }

    /**
     * 删除产品BOM信息
     *
     * @param id 产品BOM主键
     * @return 结果
     */
    @Override
    public int deleteProBomById(Long id) {
        return proBomMapper.deleteProBomById(id);
    }

    /**
     * 增-Bom
     * @param bomMtrVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult saveBom(BomMtrVo bomMtrVo) {
        //判断Bom编码是否重复
        if (RepeatUtils.isBomCodeRepeat(proBomMapper,bomMtrVo)) {
            return AjaxResult.error("Bom编码重复!");
        }


        List<MtrInfo> mtrs = bomMtrVo.getMtrs();
        for (MtrInfo mtr : mtrs) {
            ProBom proBom = new ProBom();
            proBom.setProId(bomMtrVo.getProId());
            proBom.setBomCode(bomMtrVo.getBomCode());
            proBom.setMtrId(mtr.getId());
            proBom.setMtrQuantity(mtr.getMtrQuantity());
            insertProBom(proBom);
        }

        return AjaxResult.success("新增成功！");
    }

    /**
     * 查-Bom分页
     * @param proBom
     * @return
     */
    @Override
    public List<BomListVo> selectBomListVo(ProBom proBom) {
        //页面搜索格式转换
        String s= null;
        if (proBom.getCreateTime() != null){
            s = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, proBom.getCreateTime());
        }

        List<ProBom> proBoms = proBomMapper.selectBomListVo(proBom,s);
        List<BomListVo> bomListVos = new ArrayList<>();
        if (proBoms.size() != 0){
            for (ProBom bom : proBoms) {
                BomListVo bomListVo = new BomListVo();
                bomListVo.setId(bom.getId());
                bomListVo.setBomCode(bom.getBomCode());
                bomListVo.setCreateTime(bom.getCreateTime());
                bomListVo.setRemark(bom.getRemark());

                ProInfo proInfo = proInfoMapper.selectProInfoById(bom.getProId());
                bomListVo.setProCode(proInfo.getProCode());
                bomListVo.setProName(proInfo.getProName());

                //计算成本
                BigDecimal totalCost = new BigDecimal("0.0");
                ProBom proBom1 = new ProBom();
                proBom1.setBomCode(bom.getBomCode());
                List<ProBom> proBoms1 = proBomMapper.selectProBomList(proBom1);
                for (ProBom bom1 : proBoms1) {
                    BigDecimal result = null;
                    MtrInfo mtrInfo = mtrInfoMapper.selectMtrInfoById(bom1.getMtrId());
                    result = BigDecimal.valueOf(bom1.getMtrQuantity()).multiply(mtrInfo.getCost());
                    totalCost = totalCost.add(result);
                }
                bomListVo.setTotalCost(totalCost);

                bomListVos.add(bomListVo);
            }
        }

        return PageInfoUtils.toPageInfo(proBoms,bomListVos);
    }

    /**
     * 改-Bom(先删后增)
     * @param bomMtrVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult editBom(BomMtrVo bomMtrVo) {
        //删除原有工艺编码对应原料
        proBomMapper.deleteByBomCode(bomMtrVo.getBomCode());
        //新增
        List<MtrInfo> mtrs = bomMtrVo.getMtrs();
        for (MtrInfo mtr : mtrs) {
            ProBom proBom = new ProBom();
            proBom.setProId(bomMtrVo.getProId());
            proBom.setBomCode(bomMtrVo.getBomCode());
            proBom.setMtrId(mtr.getId());
            proBom.setMtrQuantity(mtr.getMtrQuantity());
            proBom.setRemark(bomMtrVo.getRemark());
            insertProBom(proBom);
        }
        return AjaxResult.success("修改成功！");
    }

    /**
     * 删-Bom-Bom编码
     * @param bomCode
     * @return
     */
    @Override
    @Transactional
    public AjaxResult deleteByBomCode(String bomCode) {

        ProBom proBom = new ProBom();
        proBom.setBomCode(bomCode);
        List<ProBom> proBoms = proBomMapper.selectProBomList(proBom);
        Long proId = proBoms.get(0).getProId();
        //删除修改产品Bom状态
        ProInfo proInfo = proInfoMapper.selectProInfoById(proId);
        proInfo.setBomStatus(0L);
        proBomMapper.deleteByBomCode(bomCode);
        proInfoMapper.updateProInfo(proInfo);
       return AjaxResult.success("删除成功！");
    }

    /**
     * 查询产品id相同的Bom集合
     * @param id
     * @return
     */
    @Override
    public List<ProBom> getAlikeBomByProId(Long id) {
        return proBomMapper.getAlikeBomByProId(id);
    }

    /**
     * 增 - Bom - 产品是否有Bom
     * @param bomMtrVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult addBom(BomMtrVo bomMtrVo) {
        //判断Bom编码是否重复
        if (RepeatUtils.isBomCodeRepeat(proBomMapper,bomMtrVo)) {
            return AjaxResult.error("Bom编码重复!");
        }
        //判断产品是否有Bom
        ProInfo proInfo = proInfoMapper.selectProInfoById(bomMtrVo.getProId());
        if (proInfo.getBomStatus() == 1){
            return AjaxResult.error("该产品已有Bom！");
        }

        List<MtrInfo> mtrs = bomMtrVo.getMtrs();
        for (MtrInfo mtr : mtrs) {
            ProBom proBom = new ProBom();
            proBom.setProId(bomMtrVo.getProId());
            proBom.setBomCode(bomMtrVo.getBomCode());
            proBom.setMtrId(mtr.getId());
            proBom.setRemark(bomMtrVo.getRemark());
            proBom.setMtrQuantity(mtr.getMtrQuantity());
            insertProBom(proBom);
        }
        proInfo.setBomStatus(1L);
        proInfoMapper.updateProInfo(proInfo);

        return AjaxResult.success("新增成功！");
    }

    /**
     * 查 - Bom详情 - Bomid
     * @param id
     * @return
     */
    @Override
    public BomMtrVo getDetails(Long id) {
        BomMtrVo bomMtrVo = new BomMtrVo();

        ProBom proBom = proBomMapper.selectProBomById(id);
        bomMtrVo.setProId(proBom.getProId());
        bomMtrVo.setRemark(proBom.getRemark());
        bomMtrVo.setBomCode(proBom.getBomCode());

        List<ProBom> bomList = proBomMapper.getAlikeBomByProId(proBom.getProId());

        List<MtrInfo> mtrInfos = new ArrayList<>();
        for (ProBom bom : bomList) {
            MtrInfo mtrInfo = mtrInfoMapper.selectMtrInfoById(bom.getMtrId());
            mtrInfo.setMtrQuantity(bom.getMtrQuantity());
            mtrInfos.add(mtrInfo);
        }
        bomMtrVo.setMtrs(mtrInfos);
        return bomMtrVo;
    }

}
